package com.corner.ui.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.corner.R;
import com.corner.app.App;
import com.corner.presenter.imp.SelectLocationPresenter;
import com.corner.ui.adapter.SelectLocationAdapter;
import com.corner.ui.base.BaseActivity;
import com.corner.ui.view.ISelectLocationView;
import com.corner.utils.AppManager;
import com.corner.utils.view.FindView;
import com.corner.utils.view.MainActionBar;
import com.corner.utils.view.widget.XListView;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.ObjectAnimator;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/7/23，22.39.
 * 描述：
 */
public class SelectLocationActivity extends BaseActivity<SelectLocationPresenter> implements ISelectLocationView, View.OnClickListener, AdapterView.OnItemClickListener {

    private MainActionBar actionBar;
    private View rlSearch;
    @FindView(R.id.lv_location_list)
    private XListView lvLocations;
    private SelectLocationAdapter adapter;
    private List<PoiItem> poiItems;

    @Override
    protected SelectLocationPresenter createP() {
        return new SelectLocationPresenter();
    }

    @Override
    protected void setOnClick() {
        lvLocations.addHeaderView(rlSearch);
        actionBar.btnRight.setOnClickListener(this);
        rlSearch.findViewById(R.id.et_select_location_search).setOnClickListener(this);
        lvLocations.setPullRefreshEnable(false);
        lvLocations.setAutoLoadEnable(true);
        lvLocations.setOnItemClickListener(this);
        lvLocations.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                presenter.requestLocations(App.application);
            }
        });
    }

    @Override
    protected void initData() {
        presenter.requestLocations(App.application);
        adapter = new SelectLocationAdapter(new ArrayList<PoiItem>(), this);
        lvLocations.setAdapter(adapter);
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_select_location);
    }

    @Override
    public void initView() {
        super.initView();
        actionBar = new MainActionBar(this);
        actionBar.initActionBarButton();
        actionBar.btnRight.setVisibility(View.VISIBLE);
        actionBar.btnRight.setText(getString(R.string.send_info_cancel));
        actionBar.ivRight.setVisibility(View.GONE);
        initHeader();
    }

    /**
     * 初始化头布局
     */
    private void initHeader() {
        rlSearch = View.inflate(this, R.layout.view_location_list_header, null);
    }

    @Override
    public void showPois(List<PoiItem> poiItems) {
        this.poiItems = poiItems;
        if (adapter == null) {
            adapter = new SelectLocationAdapter(poiItems, this);
            lvLocations.setAdapter(adapter);
        } else {
            adapter.changeList(poiItems);
        }
    }

    @Override
    public void setLoadMoreEnable(boolean enable) {
        lvLocations.setPullLoadEnable(enable);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.et_select_location_search:
                ObjectAnimator animator = ObjectAnimator.ofFloat(this.getWindow().getDecorView(), "translationY", -actionBar.viewActionBar.getHeight());
                animator.setDuration(300);
                animator.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        Intent intent = new Intent(SelectLocationActivity.this, SearchLocationActivity.class);
                        intent.putExtra(SearchLocationActivity.INITENT_ID, CreateInfoActivity.SELECT_LOCATION);
                        startActivityForResult(intent, CreateInfoActivity.SELECT_LOCATION);
                        overridePendingTransition(0, 0);
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                });
                animator.start();
                break;
            case R.id.btn_actionbar_right:
                setResult(CreateInfoActivity.SELECT_LOCATION);
                AppManager.getInstance().finishActivity(this);
                break;
        }


    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (poiItems == null || id<0) {
            return;
        }
        Intent intent = getIntent();
        PoiItem item = poiItems.get((int) id);
        LatLonPoint point = item.getLatLonPoint();
        intent.putExtra(CreateInfoActivity.SELECT_LOCATION_RESULT_POI, new com.corner.ui.parcel.PoiItem(point.getLatitude(), point.getLongitude(), item.getSnippet(), item.getTitle(), item.getProvinceName(), item.getCityName()));
        setResult(CreateInfoActivity.SELECT_LOCATION, intent);
        AppManager.getInstance().finishActivity(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        setResult(CreateInfoActivity.SELECT_LOCATION, data);
        AppManager.getInstance().finishActivity(this);
    }
}
